<template>
  <div>
    <div :style="{
      'padding-bottom':'40px'
    }">
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="3"
        :formType="type"
        labelWidth="90"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">返 回</el-button>
      <el-button @click="saveDialog" type="primary">保 存</el-button>
    </div>
    <BaseDialog
        :visible.sync="visible"
        title="销售订单选择"
        destroyOnClose
        width="850px"
        :containerStyle="{height: '100%', overflow: 'auto'}">
        <ViNoSelect @closeViNo="closeViNoHandler"></ViNoSelect>
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import moment from 'moment'
import ViNoSelect from './viNoSelect'
import { qryDeliveryDetailBySoVinId, qryDeliverySelectPage, deliveryRegister } from '@/api/retail/salesmanage/deliveryManagement';

export default {
  name: "preDeliveryRegister",
  mixins: [dictionary],
  components: {
    ViNoSelect
  },
  props: {
    type: '',
    rowData: {}
  },
  data () {
    return {
      formList: this.createFormList(),
      saveData: null,
      whiteTimeList: ['deliveringDateStart', 'deliveringDateEnd'],
      visible: false,
      rowDataClone: {}
    }
  },
  mounted () {
    let { type, rowData } = this
    if (type != 'add') {
      this.rowDataClone = JSON.parse(JSON.stringify(rowData))
      // 编辑-查询
      this.init(rowData.soVinId)
    }
  },
  methods: {
    init (soVinId) {
      let { whiteTimeList } = this
      qryDeliveryDetailBySoVinId({'soVinId': soVinId }).then(res => {
        if (res.success) {
          this.formList.forEach(item => {
            if (whiteTimeList.includes(item.fieldName)) {
              item.initialValue = res.data[item.fieldName]?Number(res.data[item.fieldName]):null
            } else {
              item.initialValue = res.data[item.fieldName]
            }
          })
        }
      })
    },
    createFormList () {
      // 个人/公司
      let resultList = [
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'viNo',
          placeholder: '请选择',
          initialValue: '',
          disabled: ['edit','show'].includes(this.type) ? true: false,
          readonly: true,
          rules: [{ required: true, message: '请选择销售订单号', trigger: 'blur' }],
          unitRender: props => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.viNoSelect}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '',
          initialValue: '',
          disabled: true,
          rules: [{ required: true, message: '请选择有客户名称的销售订单', trigger: 'blur'}],
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'mobilePhone',
          placeholder: '',
          initialValue: '',
          disabled: true,
        },
        {
          type: 'SELECT',
          label: '交车地点',
          fieldName: 'confirmedAddress',
          placeholder: '',
          initialValue: '',
          itemList: [],
          rules: [{ required: true, message: '请输入交车地点', trigger: 'blur'}],
        },
        {
          type: 'DATE',
          label: '交车开始日期',
          fieldName: 'deliveringDateStart',
          placeholder: '',
          initialValue: '',
          labelWidth: '100',
          style: {'display':'block','width': 'auto','margin-left':'100px'},
        },
        {
          type: 'DATE',
          label: '交车结束日期',
          fieldName: 'deliveringDateEnd',
          placeholder: '',
          initialValue: '',
          rules: [{ required: true, message: '请选择交车结束日期', trigger: 'blur'}],
          labelWidth: '100',
          style: {'display':'block','width': 'auto','margin-left':'100px'},
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'salesVin',
          placeholder: '',
          initialValue: '',
          disabled: true,
          rules: [{ required: true, message: '请选择有VIN的销售订单', trigger: 'blur'}],
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'seriesName',
          placeholder: '',
          initialValue: '',
          disabled: true,
        },
        {
          type: 'SELECT',
          label: '交车标识',
          fieldName: 'handCarStatus',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('1434')
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 2,
          maxlength: 120,
          placeholder: '',
          initialValue: '',
        }
      ]
      console.log('createFormList',resultList)
      return resultList
    },
    viNoSelect () {
      console.log('选择销售订单号')
      this.visible = true;
    },
    closeViNoHandler (val) {
      let whiteFieldNameList = [
        'viNo','salesVin','customerName','seriesName','mobilePhone'
      ]
      console.log('suprise', val)
      this.rowDataClone['soVinId'] = val[0]['soVinId']
      this.formList.forEach(item => {
        if (whiteFieldNameList.includes(item.fieldName)) {
          item.initialValue = val[0][item.fieldName]
        }
      })
      this.visible = false;
    },
    formChangeHandle(val) {
      this.saveData = _.cloneDeep(val);
    },
    saveDialog () {
      let { type, rowDataClone } = this
      this.$refs.formPanel.SUBMIT_FORM();
      console.log(this.saveData)
      if (this.saveData != null) {

        


        let deliveringDateStart = this.saveData['deliveringDateStart']?new Date(this.saveData['deliveringDateStart']).getTime():''
        let deliveringDateEnd = new Date(this.saveData['deliveringDateEnd']).getTime()
        let params = {
          confirmedAddress: this.saveData['confirmedAddress'],
          handCarStatus: this.saveData['handCarStatus'],
          remark: this.saveData['remark'],
          deliveringDateStart: deliveringDateStart,
          deliveringDateEnd: deliveringDateEnd,
          soVinId: rowDataClone.soVinId
        }

        console.log("currentDate",`${moment(params.deliveringDateEnd).format('YYYY-MM-DD hh:mm:ss')}`)
        if(`${moment(params.deliveringDateEnd).format('YYYY-MM-DD hh:mm:ss')}` < `${moment(params.deliveringDateStart).format('YYYY-MM-DD hh:mm:ss')}`) {
          this.$message({
            message: '交车结束日期不可以在交车开始日期之前',
            type: 'info'
          });
        }else{
          deliveryRegister(params).then(res => {
            if (res.success) {
              this.closeDialog('refresh')
            }
          })
        }
      }
    },
    closeDialog (val) {
      this.$emit('close', val)
    },
    // 日期预处理
    preDateHandler(val, formatCode='YYYY-MM-DD HH:mm:ss') {
      let resultDate = val ? moment(Number(val)).format(formatCode) : val;
      if ((resultDate == 'Invalid date' || resultDate == 'Invalid Date') && val) {
        resultDate = moment(val).format(formatCode)
      }
      return resultDate;
    },
  }
}
</script>

<style lang="less" scoped>

</style>
